var ClubUserDataTab = Item.extend({
    _ccsFileName: res.ClubUserDataTab1_json,
    _clubId: null,
    _currentSelectView: 0,
    _groupsData: null,

    _selectPos: 0,
    _selectedMapIdStr: '',
    _selectGroupPlayers: null,
    _selectStartData: null,   //选择的查询起始时间
    _selectEndData: null,    //选择的查询结束时间
    _scoreArea: '',
    _selectRoomNumType: null,
    _selectOverviewSort: null,

    _tableViewList: [],
    initItem: function (clubId) {

        this._clubId = clubId;
        this._refreshGroupBtnView();
        this._initTableView();

        this._initDropDownMenu();

        this._nodeList['userOverviewNode.btn_sortNode0'].index = 0;
        this._nodeList['userOverviewNode.btn_sortNode1'].index = 1;
        this._nodeList['userOverviewNode.btn_sortNode2'].index = 2;

        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        var day = today.getDate();
        var arr = ClubDataManager.dateFromat(year, month, day);
        this._nodeList['dateEndNode.dateLabel'].setString(arr[0] + '/' + arr[1] + '/' + arr[2]);
        this._nodeList['dateStartNode.dateLabel'].setString(arr[0] + '/' + arr[1] + '/' + arr[2]);
        this._nodeList['dangweiLabel'].setString('分数:不限');

        this._selectStartData = this._nodeList['dateStartNode.dateLabel'].getString().replace(/\//g, '-');
        this._selectEndData = this._nodeList['dateEndNode.dateLabel'].getString().replace(/\//g, '-');
        this._scoreArea = '0,999999';
        this._selectGroupPlayers = null;
        this._selectPos = null;
        this._selectedMapIdStr = null;

        this._currentSelectView = 0;
        this._groupsData = {};
        clubMsgManager.requestGroups(this._clubId);
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'seeGroup': '_clickSeeGroup',
            'btn_allWanfa': '_clickWanFaFilter',
            'btn_edit': '_clickEdit',
            'dateStartNode': '_clickDate',
            'dateEndNode': '_clickDate',

            'searchNode': '_clickSerarchIdBg',
            'btn_search': '_clickSerarchId',
            'btn_cancel': '_clickCancelID',
            'btn_help': '_clickHelpHandler',

            'userOverviewNode.btn_sortNode0': '_overviewSortHandler',
            'userOverviewNode.btn_sortNode1': '_overviewSortHandler',
            'userOverviewNode.btn_sortNode2': '_overviewSortHandler',
        };


        for (var i = 2; i <= 9; i++) {
            if (this._nodeList['userInfoNode.roomNum' + i]) {
                this._nodeList['userInfoNode.roomNum' + i].index = i;
                this._clickListeners['userInfoNode.roomNum' + i] = '_roomNumSort';
            }
        }
    },

    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubGroupSelectEvent] = '_selectGroupHandler';
        this._eventListeners[ClubUserEditOverEvent] = '_clubEditHanlder';
        this._eventListeners[ClubEvent.FloorAndWanfaFilterEvent] = '_clubWafnaHanlder';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubRecordCode.UserCoefficient] = '_userCoefficientHandler';
        this._eventListeners[ClubRecordCode.UserChangCi] = '_userChangCiHandler';
    },

    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },

    /**
     * 初始化下拉菜单
     * @private
     */
    _initDropDownMenu: function () {
        var config = {
            selectBgRes: res.dropDownMenuTopBack,
            selectBtnRes: res.dropDownMenuBtn,
            menuRes: res.dropDownMenuBack1,
            normalIndex: 0,
            fontSize: 20,
            dataArr: ['场次数据', '参与系数'],
            selectCallBack: this._dropDownMenuHandler.bind(this)
        };
        this._dropDownMenu = new DropDownMenu(config);
        this._nodeList['menu'].getParent().addChild(this._dropDownMenu);
        this._dropDownMenu.setPosition(this._nodeList['menu'].getPosition());
    },

    _dropDownMenuHandler: function (content, index) {
        this._currentSelectView = index;
        this._changeView();
    },


    _clickSeeGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_See, this._clubId);
    },

    _clickWanFaFilter: function () {
        showWindow('ClubFloorsWanfaFilterWindow', this._clubId);
    },

    _clickEdit: function () {
        showWindow('ClubUserEditScoreWindow');
    },

    _clickDate: function (target) {
        var today = new Date();
        var curyear = today.getFullYear();
        var curmonth = today.getMonth() + 1;
        var curday = today.getDate();
        var handler = function (year, month, day) {
            var arr = ClubDataManager.dateFromat(year, month, day);
            if (target.getName() == 'dateEndNode') {
                this._nodeList['dateEndNode.dateLabel'].setString(arr[0] + '/' + arr[1] + '/' + arr[2]);
            } else {
                this._nodeList['dateStartNode.dateLabel'].setString(arr[0] + '/' + arr[1] + '/' + arr[2]);
            }
            this._selectedTimeType = null;
            this._selectStartData = this._nodeList['dateStartNode.dateLabel'].getString().replace(/\//g, '-');
            this._selectEndData = this._nodeList['dateEndNode.dateLabel'].getString().replace(/\//g, '-');
            this._changeFilter();
        };
        var _clubPowerDate = null;
        if (target.getName() == 'dateEndNode') {
            var startStr = this._nodeList['dateStartNode.dateLabel'].getString().split('/');
            _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, handler.bind(this), [startStr[0], startStr[1], startStr[2]], null, 1);
        } else {
            _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, handler.bind(this), null, null, 1);
        }
        _clubPowerDate.x = -400;
        _clubPowerDate.getChildByName('Node').getChildByName('modalLayer').setOpacity(0);
        this.addChild(_clubPowerDate);
    },

    /**
     * 当前选中分组
     * @private
     */
    _selectGroupHandler: function (data) {
        if (data['type'] && data['type'] == ClubGroupBtn_All) {
            this._selectGroupPlayers = null;
        } else {
            this._selectGroupPlayers = data['uids'].join(',');
        }
        this._refreshGroupBtnView(data['name']);
        this._changeFilter();
    },

    _clubEditHanlder: function (data) {
        var leftValue = data['leftValue'] == '0' ? '0' : data['leftValue'];
        var rightValue = data['rightValue'] == '0' ? '0' : data['rightValue'];
        if (rightValue == '0') {
            rightValue = '99999'
        }
        this._nodeList['dangweiLabel'].setString('分数:' + leftValue + '-' + rightValue);
        if (leftValue == '0' && rightValue == '0') {
            this._scoreArea = '0,99999';
            this._nodeList['dangweiLabel'].setString('分数:不限');
        } else {

            this._scoreArea = leftValue + ',' + rightValue
        }
        this._changeFilter();
    },

    _clubWafnaHanlder: function (data) {
        if (!data) return;
        this._selectPos = null;
        this._selectedMapIdStr = null;
        if (data['pos']) {
            this._selectPos = data['pos'];
        } else if (data['mapId']) {
            this._selectedMapIdStr = data['mapId'];
        }
        this._changeFilter();
    },

    _clickHelpHandler: function () {

        showWindow('ClubHelpWindow',ClubHelpWindow_Type.Coefficient);
        // window.curScene.addChild(new ClubHelpLayer(5), 99);
    },

    _queryClubGroupHandler: function () {
        var groupList = ClubDataManager.getClubGroupsInfo(this._clubId);
        for (var key in groupList) {
            var uids = groupList[key]['uids'] || [];
            if (uids.indexOf(gameData.uid) >= 0) {
                this._nodeList['userInfoNode.groupName'].setString('组名:' + groupList[key]['name']);
            }
            for (var j = 0; j < uids.length; j++) {
                this._groupsData[uids[j]] = groupList[key]['name'];
            }
        }
    },

    _userCoefficientHandler: function (data) {
        cc.log('参与系数:' + JSON.stringify(data));
        this._tableViewList = data;
        this._tableView.reloadData();
    },

    _userChangCiHandler: function (data) {
        cc.log('场次数据:' + JSON.stringify(data));
        this._tableViewList = data;
        this._tableView.reloadData();
    },

    _clickSerarchIdBg: function () {
        showWindow('ClubInputWindow', '请输入玩家Id', ClubInputWindowType.InputID, this._searchUidHandler.bind(this));
    },
    _clickSerarchId: function () {
        this._changeFilter();
    },
    _clickCancelID: function () {
        this._searchUid = 0;
        this._nodeList['searchNode.tipsLabel'].setString('输入用户ID搜索');
        this._changeFilter();
    },

    _overviewSortHandler: function (target) {
        var index = target.index;
        for (var i = 0; i < 3; i++) {
            var select = i == index;
            this._nodeList['userOverviewNode.btn_sortNode' + i + '.arrowDown'].setVisible(select);
            this._nodeList['userOverviewNode.btn_sortNode' + i + '.arrowUp'].setVisible(!select);
        }
        if (this._selectOverviewSort == index) {
            var selectNode = this._nodeList['userOverviewNode.btn_sortNode' + index + '.arrowDown'];
            var arrowScaleY = selectNode.getScaleY() > 0 ? -1 : 1;
            selectNode.setScaleY(arrowScaleY);
        }
        this._selectOverviewSort = index;
    },

    _roomNumSort: function (target) {
        var index = target.index || 2;
        for (var i = 2; i <= 9; i++) {
            if (this._nodeList['userInfoNode.roomNum' + i]) {
                var select = i == index;
                this._nodeList['userInfoNode.roomNum' + i + '.selectArrow'].setVisible(select);
                this._nodeList['userInfoNode.roomNum' + i + '.unSelectArrow'].setVisible(!select);
            }
        }
        if (this._selectRoomNumType == index) {
            var selectNode = this._nodeList['userInfoNode.roomNum' + index + '.selectArrow'];
            var arrowScaleY = selectNode.getScaleY() > 0 ? -1 : 1;
            selectNode.setScaleY(arrowScaleY);
        }
        this._selectRoomNumType = index;
    },

    /**
     * 输入 uid后确定回调
     * @param inputStr
     * @private
     */
    _searchUidHandler: function (inputStr) {
        this._searchUid = inputStr;
        if (this._searchUid == undefined || this._searchUid == "") {
            this._nodeList['searchNode.tipsLabel'].setString('输入用户ID搜索');
            return;
        }
        var m_AllNum = /^[0-9]\d*$/.test(this._searchUid);
        if (!m_AllNum) {
            showAlert('请输入数字!');
            return;
        }
        this._nodeList['searchNode.tipsLabel'].setString(this._searchUid);
    },


    /**
     * 刷新界面显示
     */
    refreshView: function () {
        this._changeView();
    },

    _changeFilter: function () {
        if (this._currentSelectView == 0) {
            // clubId, score, startDate, endDate, mapids, pos, players, playerId
            clubMsgManager.requestUesrChangCi(this._clubId,
                this._selectGroupPlayers,
                this._selectStartData,
                this._selectEndData,
                this._selectedMapIdStr,
                this._scoreArea,
                this._selectPos,
                this._searchUid
            );
        } else {
            clubMsgManager.requestUserCoefficient(this._clubId,
                this._selectGroupPlayers,
                this._selectStartData,
                this._selectEndData,
                this._selectedMapIdStr,
                this._scoreArea,
                this._selectPos,
                this._searchUid
            );
        }


    },

    /**
     * 刷新分组按钮 文字显示
     * @private
     */
    _refreshGroupBtnView: function (str) {
        if (str == null) {
            str = '全部分组';
        }
        this._nodeList['seeGroup.label'].setString(str);
    },

    _changeView: function () {
        if (this._currentSelectView == 0) {
            this._overviewSortHandler({index: 0});
            this._nodeList['userInfoNode'].setVisible(false);
            this._nodeList['btn_help'].setVisible(false);

            this._nodeList['userOverviewNode'].setVisible(true);
            this._nodeList['seeGroup'].setVisible(true);
        } else {
            this._selectRoomNumType = 2;
            this._roomNumSort({index: this._selectRoomNumType});
            this._nodeList['userInfoNode'].setVisible(true);
            this._nodeList['btn_help'].setVisible(true);
            this._nodeList['userOverviewNode'].setVisible(false);

            var clubInfo = ClubDataManager.getClubData(this._clubId);
            if (gameData.uid == clubInfo['owner_uid']) {
                this._nodeList['userInfoNode.groupName'].setVisible(false);
                this._nodeList['seeGroup'].setVisible(true);
            } else if (ClubDataManager.getClubUidIsAdmin(this._clubId, gameData.uid)) {
                this._nodeList['userInfoNode.groupName'].setVisible(true);
                this._nodeList['seeGroup'].setVisible(false);
            }
        }
        this._changeFilter();
    },


    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        // var isUesrDetailsTpye = (this._currentViewType == this.UserDetailsTpye);
        var isUesrDetailsTpye = false;
        var groupName = '';
        if (this._tableViewList[idx] && this._tableViewList[idx]['playerId'] && this._groupsData && this._groupsData[this._tableViewList[idx]['playerId']]) {
            groupName = this._groupsData[this._tableViewList[idx]['playerId']];
        }
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubUserItem', cell, this._tableViewList[idx], this._clubId, groupName, isUesrDetailsTpye);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._tableViewList[idx], this._clubId, groupName, isUesrDetailsTpye);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1016, 64);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },
});